package com.cm.biz;

import com.cm.biz.db.Db;
import com.cm.etax.tianjin.Tianjin;
import com.cm.kits.EnvironmentToolkit;
import com.cm.kits.HashKits;
import com.cm.rpa.config.RpaConfig;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.nio.charset.StandardCharsets;

/**
 * @author: cm
 * @Date: 2023/2/16 10:14
 * @Description: FpTaskExcutor实现类
 */
@Slf4j
public final class FpTaskExcutor {

    /**
     * 运行实例名
     */
    private final String instanceID;
    /**
     * 本地执行 ip
     */
    private final String ip;

    /**
     * 声明天津服务
     */
    private final Tianjin tianjin;

    /**
     * 全电发票运行主步骤
     *
     * @throws Exception
     */
    public FpTaskExcutor() throws Exception {

        // 构造唯一信息
        instanceID = "%s_java_rpa".formatted(HashKits.sha256(EnvironmentToolkit.getMac().getBytes(StandardCharsets.UTF_8)));
        ip = EnvironmentToolkit.getLocalIp();
        tianjin = new Tianjin(RpaConfig.build("rpa.yaml"));

        // 1. 登陆
        tianjin.login("XXX", "XXX", "XXX");
        // 2. 切换我要办税
        WebElement wybsEle = tianjin.rpa().findElement(By.xpath(".//a[text()='我要办税']"), 10);
        tianjin.rpa().click(wybsEle);
        // frame
        WebElement iframe = tianjin.rpa().findElement(By.cssSelector("#profile > iframe"), 10);
        // 3. 税务数字账户
        tianjin.rpa().switchToFrame(iframe);
    }

}
